package com.salmon.rpc.ser;


import com.google.protobuf.InvalidProtocolBufferException;
import com.salmon.rpc.utils.JdkSerializeUtils;
import com.salmon.rpc.utils.KryoUtils;

public class SerializeCompare {

    public static void main(String[] args){
        TestData testData=new TestData();
        testData.setSn(10);

        SerializeCompare serialize = new SerializeCompare();
        serialize.jdkSerialize(testData);
        System.out.println("---------------------------------------------------------------");
        serialize.kryoTest(testData);
        System.out.println("---------------------------------------------------------------");
        serialize.protocolTest();
    }

    public void jdkSerialize(TestData testData) {
        byte[] jdkByte = null;
        TestData deSerialize = null;
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            jdkByte = JdkSerializeUtils.serialize(testData);
            deSerialize = JdkSerializeUtils.deserialize(jdkByte, TestData.class);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("jdk serialize:" + (endTime - startTime) + "ms");
    }

    public void kryoTest(TestData testData) {
        byte[] kryoByte = null;
        TestData kryObj = null;
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            kryoByte = KryoUtils.serialize(testData);
            kryObj = KryoUtils.deserialize(kryoByte, TestData.class);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("kryo serialize:" + (endTime - startTime) + "ms");
    }

    public void protocolTest(){

        com.salmon.TestData.data.Builder testData = com.salmon.TestData.data.newBuilder();
        testData.setSn(8);
        byte[] datas;
        com.salmon.TestData.data temp = null;
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            datas = testData.build().toByteArray();
            try {
                temp = com.salmon.TestData.data.parseFrom(datas);
            } catch (InvalidProtocolBufferException e) {
                e.printStackTrace();
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("protocol serialize:" + (endTime - startTime) + "ms");

    }
}
